package info.cs52.fes.ajax;

import info.cs52.fes.Application;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class AjaxHandle extends HttpServlet {

	protected EntityManager em;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		em = Application.getEntityManager();
		EntityTransaction tx = em.getTransaction();
		tx.begin();
		try {
			super.service(request, response);
			tx.commit();
		} finally {
			em.close();
		}
	}

}
